package com.jwdf.uda.ui.fragment.find;

import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.FilterListEvent;
import com.jwdf.uda.event.GuideLikeListEvent;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.event.UserInfoRefreshEvent;
import com.jwdf.uda.event.UserInfoShowEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.CollocationListAction;
import com.jwdf.uda.http.action.GuideLikeAction;
import com.jwdf.uda.http.action.GuideUnLikeAction;
import com.jwdf.uda.ui.adapter.UserInfoRecommendAdapter;
import com.jwdf.uda.ui.adapter.item.HomeRecommendItem;
import com.jwdf.uda.ui.fragment.base.SimpleRecyclerViewFragment;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/5/10.
 */
public class UserInfoSquareFragment extends SimpleRecyclerViewFragment {
    private UserInfoRecommendAdapter mRecommendAdapter;
    private PbBean.PbSignPageFilterType mFilterType;

    public static UserInfoSquareFragment instance() {
        UserInfoSquareFragment view = new UserInfoSquareFragment();
        return view;
    }

    @Override
    protected void initViews() {

        if(getUserId().equals("")){
            mDataBinding.setIsCurrentUser(false);
        }else {
            mDataBinding.setIsCurrentUser(true);
        }
        mRecommendAdapter = new UserInfoRecommendAdapter(getActivity());
        mDataBinding.commonFragmentList.setLinearLayout();
        mDataBinding.commonFragmentList.setRefreshing(true);
        mDataBinding.commonFragmentList.addHeaderView(createHeaderView());
        mDataBinding.commonFragmentList.setAdapter(mRecommendAdapter);

        EventBusUtil.registerEvent(mRecommendAdapter);
    }



    @Override
    protected void onListItemClick(int position) {
    }
    @Override
    protected void requestData(final boolean refresh) {
        if (refresh && mRecommendAdapter.isEmpty()) {
//            showProgressDialog();
        }
        int offset = refresh ? 0 : mRecommendAdapter.getCount();
        onNewRequestCall(CollocationListAction.newInstance(getContext(), getUserId(), offset, getFilterType()).request(new AHttpService.IResCallback<PbBean.PbResGuideGetGuideList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResGuideGetGuideList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
//                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
//                        mDataBinding.commonFragmentList.stopRefresh();
//                        mDataBinding.commonFragmentList.stopLoadMore();
                        mDataBinding.commonFragmentList.setPullLoadMoreCompleted();
                        EventBusUtil.post(new UserInfoRefreshEvent());
                        return;
                    }
                    doSuccessResponse(refresh, response.body());
                }
            }
        }));
    }
    protected PbBean.PbSignPageFilterType getFilterType() {
        return mFilterType;
    }
    private void doSuccessResponse(boolean refresh, PbBean.PbResGuideGetGuideList body) {
        if (body != null) {
            if (refresh) {
                mRecommendAdapter.setData(HomeRecommendItem.convert(body.getGuideListList()));
            } else {
                mRecommendAdapter.append(HomeRecommendItem.convert(body.getGuideListList()));
            }
            mRecommendAdapter.notifyDataSetChanged(HomeRecommendItem.convert(body.getGuideListList()),refresh);
                mDataBinding.commonFragmentList.setPullLoadMoreCompleted();
//            mDataBinding.commonFragmentList.setNoData(body.getGuideListList().size() == getPageLength());
            mDataBinding.commonFragmentList.setNoData(body.getGuideListList().size()==getPageLength(),body.getGuideListList().size(),getPageLength());
            if (mRecommendAdapter.getCount() == 0) {
                mDataBinding.tvEmptyData.setVisibility(View.VISIBLE);
                mDataBinding.commonFragmentList.deleteFooterView();
            }
            EventBusUtil.post(new UserInfoRefreshEvent());
//            mDataBinding.commonFragmentList.setPullLoadEnable(body.getRecommendListList().size() == getPageLength());
//            mDataBinding.commonFragmentList.stopRefresh();
//            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }
    protected String getUserId() {
        return ConfigUtils.getUserId(getContext());
    }
    private View createHeaderView() {
        View headerView = getActivity().getLayoutInflater().inflate(R.layout.view_head_divider, null);
        return headerView;
    }
    protected boolean showHeadDivider() {
        return true;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusUtil.unregisterEvent(mRecommendAdapter);
    }
    @Override
    public void onStart() {
        super.onStart();
        EventBusUtil.registerEvent(this);
    }
    @Override
    public void onStop() {
        super.onStop();
        EventBusUtil.unregisterEvent(this);
    }
    public void onEvent(FilterListEvent event) {
        mFilterType = event.menuItem == 0 ? PbBean.PbSignPageFilterType.PAGE_FILTER_TYPE_NONE : PbBean.PbSignPageFilterType.PAGE_FILTER_TYPE_FOLLOW;
        autoRefresh();
    }
    public void onEventMainThread(UserInfoShowEvent event) {
        requestData(true);
    }
    public void onEvent(IndividualEvent event) {
        autoRefresh();
    }

    public void onEventMainThread(GuideLikeListEvent event){
        switch (event.getEvent()){
            case GuideLikeListEvent.EVENT_GUIDE_LIKE:
                doLikeGuideAction(event.getmGuideId());
                break;
            case GuideLikeListEvent.EVENT_GUIDE_UNLIKE:
                doUnLikeGuideAction(event.getmGuideId());
                break;
        }
    }

    private void doLikeGuideAction(int mGuideId) {
//        showProgressDialog();
        GuideLikeAction action = GuideLikeAction.newInstance(getContext(), mGuideId);
        onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResGuideLike>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResGuideLike> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
//                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doLikeGuideResponse();
                }
            }
        }));
    }
    private void doLikeGuideResponse() {
//        mIsLike = true;
//        mIView.showPraiseView(mIsLike);
        requestData(true);
    }
    private void doUnLikeGuideAction(int mGuideId) {
//        showProgressDialog();
        GuideUnLikeAction action = GuideUnLikeAction.newInstance(getContext(), mGuideId);
        onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResGuideUnLike>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResGuideUnLike> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
//                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doUnLikeGuideResponse();
                }
            }
        }));
    }
    private void doUnLikeGuideResponse() {
//        mIsLike = false;
//        mIView.showPraiseView(mIsLike);
        requestData(true);
    }

}
